<template>
  <div class="admin-dashboard">
    <div class="dashboard-header">
      <h1>管理后台</h1>
      <p>FAIR plus机器人全产业链接会管理系统</p>
    </div>

          
      <el-row :gutter="20" style="margin-top: 30px;">
        <!-- 快捷操作 -->
        <el-col :span="24">
          <el-card>
            <template #header>
              <span>快捷操作</span>
            </template>
            <div class="quick-actions">
              <el-button 
                v-for="action in quickActions" 
                :key="action.key"
                :type="action.type"
                :icon="action.icon"
                @click="handleQuickAction(action.key)"
              >
                {{ action.label }}
              </el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
    
    <div class="dashboard-content">
      <el-row :gutter="20">
        <!-- 统计卡片 -->
        <el-col :span="6" v-for="stat in stats" :key="stat.key">
          <el-card class="stat-card clickable-card" @click="handleStatClick(stat.key)">
            <div class="stat-content">
              <div class="stat-icon" :class="stat.iconClass">
                <el-icon><component :is="stat.icon" /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-row">
                  <span class="stat-label-inline">新增：</span>
                  <span class="stat-value-new">{{ stat.newValue }}</span>
                </div>
                <div class="stat-row">
                  <span class="stat-label-inline">总数：</span>
                  <span class="stat-value-total">{{ stat.totalValue }}</span>
                </div>
                <p class="stat-name">{{ stat.label }}</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <el-row :gutter="20" style="margin-top: 30px;">
        <!-- 活动报名统计 -->
        <el-col :span="24">
          <el-card>
            <template #header>
              <span>活动报名统计</span>
            </template>
            <div class="activity-list">
              <div 
                v-for="activity in activityStats" 
                :key="activity.name" 
                class="activity-item clickable-item"
                @click="handleActivityClick"
              >
                <div class="activity-icon">
                  <el-icon><Document /></el-icon>
                </div>
                <div class="activity-content">
                  <div class="activity-title">{{ activity.name }}</div>
                  <div class="activity-stats">
                    <span class="stat-tag pending">待审核: {{ activity.pendingCount }}</span>
                    <span class="stat-tag total">总报名: {{ activity.totalCount }}</span>
                  </div>
                </div>
              </div>
              <div v-if="activityStats.length === 0" class="empty-message">
                暂无活动数据
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { dashboardApi } from '@/api/admin'
import { 
  User, 
  OfficeBuilding, 
  Avatar, 
  Document,
  Plus,
  Setting,
  Download,
  Upload
} from '@element-plus/icons-vue'

export default {
  name: 'AdminDashboard',
  components: {
    User,
    OfficeBuilding,
    Avatar,
    Document,
    Plus,
    Setting,
    Download,
    Upload
  },
  setup() {
    const router = useRouter()
    
    const stats = reactive([
      { key: 'exhibitors', label: '参展商', totalValue: 0, newValue: 0, icon: 'OfficeBuilding', iconClass: 'exhibitors' },
      { key: 'visitors', label: '展会观众', totalValue: 0, newValue: 0, icon: 'Avatar', iconClass: 'visitors' },
      { key: 'visa', label: '签证申请', totalValue: 0, newValue: 0, icon: 'Document', iconClass: 'visa' },
      { key: 'media', label: '媒体注册', totalValue: 0, newValue: 0, icon: 'User', iconClass: 'media' }
    ])
    
    const activityStats = ref([])
    
    const quickActions = reactive([
      { key: 'addNews', label: '新增资讯', icon: Plus, type: 'primary' },
      { key: 'addExhibitor', label: '添加展商', icon: Plus, type: 'success' },
      { key: 'publishNews', label: '发布活动', icon: Document, type: 'info' },
      { key: 'systemSettings', label: '系统设置', icon: Setting, type: 'warning' }
    ])
    
    const handleQuickAction = (action) => {
      switch (action) {
        case 'addNews':
          router.push('/admin/media')
          break
        case 'addExhibitor':
          router.push('/admin/exhibitors')
          break
        case 'publishNews':
          router.push('/admin/activity-content')
          break
        case 'systemSettings':
          router.push('/admin/settings')
          break
        case 'exportData':
          ElMessage.info('导出功能开发中')
          break
        case 'importData':
          ElMessage.info('导入功能开发中')
          break
      }
    }

    // 统计卡片点击跳转
    const handleStatClick = (key) => {
      switch (key) {
        case 'exhibitors':
          router.push('/admin/exhibitors')
          break
        case 'visitors':
          router.push('/admin/visitors')
          break
        case 'visa':
          router.push('/admin/visa')
          break
        case 'media':
          router.push('/admin/media-regitser')
          break
      }
    }

    // 活动报名统计点击跳转
    const handleActivityClick = () => {
      router.push('/admin/activities')
    }
    
    // 加载统计数据
    const loadStatistics = async () => {
      try {
        const response = await dashboardApi.getStatistics()
        if (response && response.data) {
          const data = response.data
          // 参展商：总数 totalExhibitors，新增 exhibitorStats.pending
          stats[0].totalValue = data.totalExhibitors || 0
          stats[0].newValue = data.exhibitorStats?.pending || 0
          
          // 展会观众：总数 totalVisitors，新增 unReadsVisitors
          stats[1].totalValue = data.totalVisitors || 0
          stats[1].newValue = data.unReadsVisitors || 0
          
          // 签证申请：总数 totalVisaApplications，新增 pendingVisaApplications
          stats[2].totalValue = data.totalVisaApplications || 0
          stats[2].newValue = data.pendingVisaApplications || 0
          
          // 媒体注册：总数 totalMediaRegistrations，新增 pendingMediaRegistrations
          stats[3].totalValue = data.totalMediaRegistrations || 0
          stats[3].newValue = data.pendingMediaRegistrations || 0

          // 活动报名统计数据
          if (data.activityStatsList && Array.isArray(data.activityStatsList)) {
            activityStats.value = data.activityStatsList
          }
        }
      } catch (error) {
        console.error('加载统计数据失败:', error)
        ElMessage.error('加载统计数据失败')
      }
    }

    // 初始化数据
    const initData = async () => {
      await loadStatistics()
    }

    onMounted(() => {
      initData()
    })
    
    return {
      stats,
      activityStats,
      quickActions,
      handleQuickAction,
      handleStatClick,
      handleActivityClick
    }
  }
}
</script>

<style scoped>
.admin-dashboard {
  padding: 20px;
  background: #f8f9fa;
  min-height: 100vh;
}

.dashboard-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 20px;
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
  color: white;
  border-radius: 10px;
}

.dashboard-header h1 {
  font-size: 28px;
  margin-bottom: 10px;
}

.dashboard-header p {
  font-size: 16px;
  opacity: 0.9;
}

.dashboard-content {
  margin-top: 20px;
}

.stat-card {
  margin-bottom: 20px;
}

.clickable-card {
  cursor: pointer;
  transition: all 0.3s ease;
}

.clickable-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(50, 0, 99, 0.15);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-icon.users {
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
}

.stat-icon.exhibitors {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.visitors {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.visa {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-icon.media {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.stat-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stat-label-inline {
  font-size: 14px;
    font-weight: bold;
  display: flex;
  align-items: center;

}

.stat-value-new {
  font-size: 24px;
  font-weight: bold;
  color: black;
}

.stat-value-total {
  font-size: 24px;
  font-weight: bold;
  color: black;
}

.stat-name {
  margin: 6px 0 0 0;
  font-size: 13px;
  color: #999;
}

.activity-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  max-height: 300px;
  overflow-y: auto;
  padding-right: 5px;
}

.activity-list::-webkit-scrollbar {
  width: 6px;
}

.activity-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.activity-list::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.activity-list::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.activity-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.activity-item:last-child {
  border-bottom: none;
}

.clickable-item {
  cursor: pointer;
  transition: background-color 0.3s ease;
  padding: 10px;
  margin: 0 -10px;
  border-radius: 6px;
}

.clickable-item:hover {
  background-color: #f5f7fa;
}

.activity-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #320063;
}

.activity-content {
  flex: 1;
}

.activity-title {
  font-weight: 500;
  color: #2c3e50;
}

.activity-stats {
  display: flex;
  gap: 15px;
  margin-top: 8px;
}

.stat-tag {
  font-size: 13px;
  padding: 4px 12px;
  border-radius: 4px;
  font-weight: 500;
}

.stat-tag.pending {
  background: #fef0f0;
  color: #f56c6c;
}

.stat-tag.total {
  background: #f0f9ff;
  color: #409eff;
}

.empty-message {
  text-align: center;
  padding: 40px 20px;
  color: #999;
  font-size: 14px;
}

.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}

.quick-actions .el-button {
  margin: 0;
}

@media (max-width: 768px) {
  .el-col-6 {
    width: 50%;
  }
  
  .el-col-12 {
    width: 100%;
  }
  
  .quick-actions {
    justify-content: center;
  }
}
</style> 